using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace GenerationCode.Utils
{
    /// <summary>
    /// Summary description for Convert.
    /// </summary>
    public class Utility
    {
        public static string ConvertType(string sqlType)
        {
            switch (sqlType.ToLower())
            {

                case "text":
                case "ntext":
                case "nchar":
                case "varchar":
                case "nvarchar":
                case "char":
                    {
                        return "string";
                    }

                case "datetime":
                    return "DateTime";

                case "int":
                case "tinyint":
                case "smallint":
                    {
                        return "int";
                    }
                case "bit":
                    {
                        return "Boolean?";
                    }
                case "bigint":
                    {
                        return "Int64";
                    }

                case "decimal":
                case "money":
                case "numeric":
                    {
                        return "decimal";
                    }
               
                case "float":
                case "real":
                    {
                        return "double";
                    }

                case "image":
                    return "byte []";
                case "uniqueidentifier":
                    return "Guid";
                case "varbinary":
                case "binary":
                    return "byte []";
            }
           
            return "object";

        }

        public static string ParseSqlType(string sqlType)
        {
            switch (sqlType.ToLower())
            {

                case "text":
                case "ntext":
                case "nchar":
                case "varchar":
                case "nvarchar":
                    {
                        return "String";
                    }
                case "char":
                    {
                        return "String";
                    }

                case "datetime":
                    return "DateTime";

                case "int":
                    return "Int";
                case "tinyint":
                case "bit":
                    {
                        return "Boolean?";
                    }
                case "smallint":
                    {
                        return "SmallInt";
                    }
                case "bigint":
                    {
                        return "BigInt";
                    }

                case "numeric":
                case "money":
                case "decimal":
                    {
                        return "Decimal";
                    }

               
                case "float":
                case "real":
                    {
                        return "Double";
                    }

                case "image":
                    return "Image";

            }
            return "object";
        }

        public static string ConvertParamType(string paramType)
        {
            switch (paramType)
            {
                case "int":
                    return "DbType.Int32";
                case "tinyint":
                    return "DbType.Byte";
                case "smallint":
                case "bit":
                    {
                        return "DbType.Boolean";
                    }
                case "bigint":
                    return "DbType.Int64";
                case "decimal":
                    return "DbType.Decimal";
                case "numeric":
                    return "DbType.Decimal";
                case "float":
                    return "DbType.Double";
                case "money":
                    return "DbType.Decimal";
                case "real":
                    return "DbType.Double";
                case "image":
                case "varbinary":
                case "binary":
                    return "DbType.Binary";
                case "text":
                    return "DbType.AnsiString";
                case "ntext":
                    return "DbType.String";
                case "nchar":
                    return "DbType.String";
                case "varchar":
                    return "DbType.AnsiString";
                case "nvarchar":
                    return "DbType.String";
                case "char":
                    return "DbType.AnsiString";
                case "datetime":
                    return "DbType.DateTime";
                case "uniqueidentifier":
                    return "DbType.Guid";                
                
            }
            return "DbType.Object";
        }

        public static DataTable GetObjects(string connectionString, string command)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(command, conn);
            SqlDataReader dr = null;
            DataTable tb = new DataTable();
            try
            {
                conn.Open();
                dr = cmd.ExecuteReader();
                int count = dr.FieldCount;
                for (int i = 0; i < count; i++)
                {
                    DataColumn col = new DataColumn();
                    tb.Columns.Add(col);
                }
                while (dr.Read())
                {
                    DataRow row = tb.NewRow();
                    for (int i = 0; i < count; i++)
                    {
                        row[i] = dr[i];
                    }
                    tb.Rows.Add(row);
                }
                return tb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                conn.Close();
            }
        }

        public static void SetKeyForTable(string connectionString,
                                                                                string tableName, string keyString)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            string command = string.Format(@"ALTER TABLE {0}
						ADD CONSTRAINT PK_{0} PRIMARY KEY ( {1} )",
                        tableName, keyString);
            SqlCommand cmd = new SqlCommand(command, conn);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }

        public static string ConvertToPropertyName(string name)
        {
            string firstLetter = name.Substring(0, 1);
            string remain = name.Substring(1, name.Length - 1);
            return firstLetter.ToUpper() + remain;
        }

        public static string ConvertToFieldName(string name)
        {
            string firstLetter = name.Substring(0, 1);
            string remain = name.Substring(1, name.Length - 1);
            return firstLetter.ToLower() + remain;
        }

        public static string InitializeDataType(string type)
        {
            if (type == "int")
                return "int.MinValue";
            if (type == "Int64")
                return "Int64.MinValue";
            if (type == "long")
                return "long.MinValue";
            if (type == "Guid")
                return "Guid.Empty";            
            if (type == "DateTime")
                return "DateTime.MinValue";
            if (type == "string")
                return "string.Empty";
            if (type == "double")
                return "double.MinValue";
            if (type == "decimal")
                return "decimal.MinValue";
            if (type == "Boolean?")
                return "null";
            return "null";
            
        }
        public static string MultilanguageTableName
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["multilanguageTableName"].ToString();
                if (s == string.Empty)
                    s = "_Trans";
                return s;
            }
        }
        public static string MultilanguageTableNameList
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["multilanguageTableNameList"].ToString();
                if (s == string.Empty)
                    s = "Collection";
                return s;
            }
        }
        public static string LanguageIDField
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["languageIDField"].ToString();
                if (s == string.Empty)
                    s = "languageId";
                return s;
            }
        }
        public static string LanguageIDFieldSQLDataType
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["languageIDFieldSQLDataType"].ToString();
                if (s == string.Empty)
                    s = "int";
                return s;
            }
        }
        public static string LanguageIDFieldDataType
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["languageIDFieldDataType"].ToString();
                if (s == string.Empty)
                    s = "int";
                return s;
            }
        }
        public static string FunctionByLanguageName
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["functionByLanguageName"].ToString();
                if (s == string.Empty)
                    s = "ByLanguage";
                return s;
            }
        }
         public static string getObjectName
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["getObjectName"].ToString();
                if (s == string.Empty)
                    s = "result";
                return s;
            }
        }
         public static string insertUpdateObjectName
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["insertUpdateObjectName"].ToString();
                if (s == string.Empty)
                    s = "obj";
                return s;
            }
        }
         public static int LanguageIDFieldSQLDataLength
        {
            get
            {
                string s = System.Configuration.ConfigurationSettings.AppSettings["languageIDFieldSQLDataLength"].ToString();
                if (s == string.Empty)
                    s = "100";
                return int.Parse(s);
            }
        }
         public static string DataAccessNamespace
         {
             get
             {
                 string s = System.Configuration.ConfigurationSettings.AppSettings["DataAccessNamespace"].ToString();
                 if (s == string.Empty)
                       s = "DataAccessLayer";
                 return s;
             }
         }
         public static string BusinessObjectNamespace
         {
             get
             {
                 string s = System.Configuration.ConfigurationSettings.AppSettings["BusinessObjectNamespace"].ToString();
                 if (s == string.Empty)
                     s = "BusinessObject";
                 return s;
             }
         }
         public static string BusinessLogicNamespace
         {
             get
             {
                 string s = System.Configuration.ConfigurationSettings.AppSettings["BusinessLogicNamespace"].ToString();
                 if (s == string.Empty)
                     s = "BusinessLogic";
                 return s;
             }
         }
         public static string UtilityNamespace
         {
             get
             {
                 string s = System.Configuration.ConfigurationSettings.AppSettings["UtilityNamespace"].ToString();
                 if (s == string.Empty)
                     s = "UtilityNamespace";
                 return s;
             }
         }
        


       

        
        

        
        
    }
}
